﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework;
using Krist_em_up.Components;

namespace Krist_em_up.Interface
{
    public class SelectKeyButton
    {
        public Keys BoundKey
        {
            get { return _boundKey; }
        }
        protected Keys _boundKey;
        protected string Label;
        protected Vector2 Position;
        protected bool Focused, Activated;
        protected Color c;
        protected GameStateManager game;

        protected Texture2D bg;

        protected Effect gradient;

        public const int MARGIN = 30;
        public bool HasFocus
        {
            get { return Focused; }
        }

        public bool IsActive
        {
            get { return Activated; }
        }

        public SelectKeyButton(Vector2 position, string text, Keys DefaultKey, GameStateManager g)
        {
            _boundKey = DefaultKey;
            Label = text;
            Position = position;
            c = Color.Red;
            Focused = false;
            Activated = false;
            game = g;
            bg = game.Content.Load<Texture2D>("pixel");
            gradient = game.Content.Load<Effect>("Menu/grad");
        }
        public void Update(Clavier kb, GameTime gametime, OptionsMenu opM)
        {
            if (!Activated && kb.HasBeenPressed(Keys.Enter))
            {
                Activate();
                c = Color.Green;
            }
            else if (Activated)
            {
                Keys? k = kb.GetPressedKey();
                if (k.HasValue)
                {
                    _boundKey = (Keys) k;
                    Desactivate();
                    opM.UpdateLastAction(gametime);
                }
            }
        }

        private void Activate()
        {
            Activated = true;
            c = Color.Green;
        }
        private void Desactivate()
        {
            Activated = false;
            c = HasFocus ? Color.Pink : Color.Red;
        }
        public void Draw(SpriteBatch sprite, SpriteFont font)
        {
            int mid = game.GraphicsDevice.Viewport.Width / 2;

            sprite.End();

            sprite.Begin(SpriteBlendMode.AlphaBlend, SpriteSortMode.Immediate, SaveStateMode.None);
            gradient.Begin();
            gradient.CurrentTechnique.Passes[0].Begin();
            Vector2 size = font.MeasureString(_boundKey.ToString());
            sprite.Draw(bg, new Rectangle(mid , (int)Position.Y, (int)size.X + MARGIN, (int)size.Y), Color.White);

            sprite.End();
            gradient.End();
            gradient.CurrentTechnique.Passes[0].End();

            sprite.Begin();
            sprite.DrawString(font, Label, new Vector2(mid - font.MeasureString(Label).X - MARGIN/2, Position.Y), c);
            sprite.DrawString(font, _boundKey.ToString(), new Vector2(mid + MARGIN/2, Position.Y), c);
        }
        public void Focus()
        {
            Focused = true;
            c = Color.Pink;
        }
        public void Unfocus()
        {
            Focused = false;
            c = Color.Red;
        }
    }
}
